import 'package:flame/components.dart';
import 'package:flutter/material.dart';

import 'bird.dart';

class ScoreComponent extends PositionComponent {
  ScoreComponent(this.screenSize, this.birdComponent)
      : super(size: Vector2(120, 44));

  Vector2 screenSize;
  final BirdComponent birdComponent;

  late TextComponent scoreText;

  @override
  Future<void>? onLoad() async {
    scoreText = TextComponent(
      textRenderer: TextPaint(
        style: const TextStyle(
            fontFamily: 'game_font',
            fontSize: 30,
            color: Colors.white,
            shadows: [
              Shadow(color: Colors.black, offset: Offset(1, 1), blurRadius: 1),
            ]),
      ),
    );
    scoreText.text = "0";
    scoreText.position = Vector2(0, 0);
    add(scoreText);
  }

  @override
  void onGameResize(Vector2 size) {
    super.onGameResize(size);
    screenSize = size;
    position = Vector2((screenSize.x - 24) / 2, 120);
  }

  @override
  void update(double dt) {
    super.update(dt);
    if (birdComponent.getCurrentStatus() == BirdComponent.statusBeforeGame) {
      removeFromParent();
    } else {
      position = Vector2((screenSize.x - 24) / 2, 120);
    }
  }

  void setScore(int score) {
    scoreText.text = score.toString();
  }
}
